package com.tozed.android.launcher.simpleui;

import com.tozed.android.launcher.R;

import android.content.Context;
import android.content.res.Resources;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewDebug;

public class SimpleCellLayout extends ViewGroup
{
	public static final int FOLDER_COUNTX = 4;
	public static final int FOLDER_COUNTY = 4;
	public static final int SIMPLEUI_COUNTX = 2;
	public static final int SIMPLEUI_COUNTY = 4;
	private int mCellHeight;
	private int mCellWidth;
	private int mCurrentCountX = SIMPLEUI_COUNTX;
	private int mCurrentCountY = SIMPLEUI_COUNTY;
	private int mHeightGap;
	private boolean[][] mOccupied; // 已占用的
	private static int mPaddingLeft;
	private static int mPaddingTop;
	private int mWidthGap;
	  
	public SimpleCellLayout(Context context){
		super(context, null);
	}

	public SimpleCellLayout(Context context, AttributeSet attrs){
		super(context, attrs, 0);
		setPaddingLeft(getPaddingLeft());
		setPaddingTop(getPaddingTop());
		
		// 布局为2行4列模式
		mOccupied = new boolean[mCurrentCountX][mCurrentCountY];
	}

	public SimpleCellLayout(Context context, AttributeSet attrs, int defStyle){
		super(context, attrs, defStyle);
	}
	
	public boolean addViewToCellLayout(View childView, int index, LayoutParams params, boolean markCells)
	{
		if ((params.cellX >= 0) && (params.cellX <= mCurrentCountX - 1) 
			&& (params.cellY >= 0) && (params.cellY <= mCurrentCountY - 1))
		{
			if (params.cellHSpan < 0)
				params.cellHSpan = mCurrentCountX;
			if (params.cellVSpan < 0)
				params.cellVSpan = mCurrentCountY;
			
			addView(childView, index, params);
			return true;
		}
		
		return false;
	}
	
	public void clearOccupiedCells()
	{
		for (int x = 0; x < SIMPLEUI_COUNTX; x++) {
            for (int y = 0; y < SIMPLEUI_COUNTY; y++) {
                mOccupied[x][y] = false;
            }
        }
	}
	
	public int getCellLayoutBottom(boolean isScale, Context context)
	{
		// 编辑模式缩小比例
		Resources res = context.getResources();
		float editModeShrinkPercentage = res.getInteger(R.integer.config_simpleWorkspaceEditmodeShrinkPercentage)/100.0F;
		if (isScale)
			return (int)(mPaddingTop + editModeShrinkPercentage * ((mCurrentCountY - 1) * mHeightGap 
						+ mCurrentCountY * mCellHeight));
		
		return mPaddingTop + (mCurrentCountY - 1) * mHeightGap + mCurrentCountY * mCellHeight;
	}
	
	public View getChildAt(int x, int y)
	{
		int count = getChildCount();
		for (int i = 0; i < count; i++)
	    {
	      View view = getChildAt(i);
	      LayoutParams params = (LayoutParams)view.getLayoutParams();
	      if ((params.cellX <= x) && (x < params.cellX + params.cellHSpan) 
	    	 && (params.cellY <= y) && (y < params.cellY + params.cellVSpan)){
	        return view;
	      }
	    }
		
	    return null;
	}
	
	public int getCountX(){
		return mCurrentCountX;
	}

	public int getCountY(){
		return mCurrentCountY;
	}

	public boolean[][] getOccupied(){
		return mOccupied;
	}
	
	public void measureChild(View view)
	{
		LayoutParams params = (LayoutParams) view.getLayoutParams();
		params.setup(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
		view.measure(View.MeasureSpec.makeMeasureSpec(params.width, MeasureSpec.EXACTLY),
						  View.MeasureSpec.makeMeasureSpec(params.height, MeasureSpec.EXACTLY));
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b)
	{
		int count = getChildCount();
		for (int i = 0; i < count; i++)
		{
			View child = getChildAt(i);
			if (child.getVisibility() == View.GONE)
				continue;
			
			LayoutParams params = (LayoutParams)child.getLayoutParams();
			child.layout(params.x, params.y, params.x + params.width, params.y + params.height);
		}
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
	{
		int sizeWidth = MeasureSpec.getSize(widthMeasureSpec); 	// LCD分辨率的宽度
		int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);// LCD分分辨率的高度

		// 子视图测量
		int count = getChildCount();
		for (int i = 0; i < count; i++)
			measureChild(getChildAt(i));
		
		// SimpleCellLayout自己的宽高测量值, 按非wrap_content来计算
		setMeasuredDimension(sizeWidth, sizeHeight);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event)
	{
		return true;
	}
	
	public void setCellDimensions(int cellWidth, int cellHeight, int widthGap, int heightGap)
	{
		this.mCellWidth = cellWidth;
		this.mCellHeight = cellHeight;
		this.mWidthGap = widthGap;
		this.mHeightGap = heightGap;
		this.mCurrentCountX = 4; // 4X 4Y的Cell用于显示其它App和下载App文件夹时
		this.mCurrentCountY = 4;
	}
	
	public void setOccupied(int x, int y){
		mOccupied[x][y] = true;
	}

	public void setOccupied(int x, int y, boolean flag){
		mOccupied[x][y] = flag;
	}
	
	public void setPaddingLeft(int paddingLeft){
		mPaddingLeft = paddingLeft;
	}

	public void setPaddingTop(int paddingTop){
		mPaddingTop = paddingTop;
	}
	
	public static class LayoutParams extends ViewGroup.MarginLayoutParams
	{
		@ViewDebug.ExportedProperty
	    public int cellHSpan;

	    @ViewDebug.ExportedProperty
	    public int cellVSpan;

	    @ViewDebug.ExportedProperty
	    public int cellX;

	    @ViewDebug.ExportedProperty
	    public int cellY;
	    public boolean isLockedToGrid = true;

	    @ViewDebug.ExportedProperty
	    int x;

	    @ViewDebug.ExportedProperty
	    int y;
	    
		public LayoutParams(Context c, AttributeSet attrs){
			super(c, attrs);
			cellHSpan = 1;
			cellVSpan = 1;
		}

		public LayoutParams(ViewGroup.LayoutParams source){
			super(source);
			cellHSpan = 1;
			cellVSpan = 1;
		}

		public LayoutParams(LayoutParams source){
			super(source);
			this.cellX = source.cellX;
			this.cellY = source.cellY;
			this.cellHSpan = source.cellHSpan;
			this.cellVSpan = source.cellVSpan;
		}
		
		public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan){
			super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
			this.cellX = cellX;
			this.cellY = cellY;
			this.cellHSpan = cellHSpan;
			this.cellVSpan = cellVSpan;
		}
		
		public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap)
		{
			if (isLockedToGrid)
			{
				final int myCellHSpan = cellHSpan;
				final int myCellVSpan = cellVSpan;
				
				width = (myCellHSpan * cellWidth + widthGap * (myCellHSpan - 1) - leftMargin - rightMargin);
				height = (myCellVSpan * cellHeight + heightGap * (myCellVSpan - 1) - topMargin - bottomMargin);
				x = (cellX * (cellWidth + widthGap) + mPaddingLeft);
				y = (cellY * (cellHeight + heightGap) + mPaddingTop);
			}
		}

		public String toString() {
            return "(" + this.cellX + ", " + this.cellY + ")";
        }
		
		public int getHeight(){
			return this.height;
		}

		public int getWidth(){
			return this.width;
		}

		public int getX(){
			return this.x;
		}

		public int getY(){
			return this.y;
		}

		public void setHeight(int height){
			this.height = height;
		}

		public void setWidth(int width){
			this.width = width;
		}

		public void setX(int x){
			this.x = x;
		}

		public void setY(int y){
			this.y = y;
		}
	}
}
